package com.dongdongshop.service.impl;

import com.alibaba.dubbo.config.annotation.Service;
import com.dongdongshop.mapper.TbAddressMapper;
import com.dongdongshop.mapper.TbAreasMapper;
import com.dongdongshop.mapper.TbCitiesMapper;
import com.dongdongshop.mapper.TbProvincesMapper;
import com.dongdongshop.pojo.*;
import com.dongdongshop.service.AddressService;

import javax.annotation.Resource;
import java.util.List;

@Service
@org.springframework.stereotype.Service
public class AddressServiceImpl implements AddressService {

    @Resource
    private TbAddressMapper tbAddressMapper;

    @Resource
    private TbProvincesMapper tbProvincesMapper;

    @Resource
    private TbCitiesMapper tbCitiesMapper;

    @Resource
    private TbAreasMapper tbAreasMapper;

    @Override
    public List<TbAddress> getAddressListByUser(String username) {
        TbAddressExample tae = new TbAddressExample();
        TbAddressExample.Criteria criteria = tae.createCriteria();
        criteria.andUserIdEqualTo(username);
        tae.setOrderByClause(" is_default ");
        return tbAddressMapper.selectByExample(tae);
    }

    @Override
    public String getProvinceById(String provinceId) {
        return tbProvincesMapper.selectProvinceByProvinceId(provinceId);
    }

    @Override
    public String getCityById(String cityId) {
        return tbCitiesMapper.selectCityByCityId(cityId);
    }

    @Override
    public String getAreaById(String townId) {
        return tbAreasMapper.selectAreaByTownId(townId);
    }

    @Override
    public List<TbProvinces> getProvincesList() {
        return tbProvincesMapper.selectByExample(new TbProvincesExample());
    }

    @Override
    public List<TbCities> listCityByProvinceId(String provinceId) {
        TbCitiesExample tbCitiesExample = new TbCitiesExample();
        TbCitiesExample.Criteria criteria = tbCitiesExample.createCriteria();
        criteria.andProvinceidEqualTo(provinceId);
        return tbCitiesMapper.selectByExample(tbCitiesExample);
    }

    @Override
    public List<TbAreas> listAreaByCityId(String cityId) {
        TbAreasExample tbAreasExample = new TbAreasExample();
        TbAreasExample.Criteria criteria = tbAreasExample.createCriteria();
        criteria.andCityidEqualTo(cityId);
        return tbAreasMapper.selectByExample(tbAreasExample);
    }

    @Override
    public int insertAddress(TbAddress tbAddress) {
        return tbAddressMapper.insertSelective(tbAddress);
    }

    @Override
    public int updateDefaultAddress(String username, Long id) {
        tbAddressMapper.updateDefaultAddressByUserName(username);
        return tbAddressMapper.updateDefaultAddressById(id);
    }

    @Override
    public TbAddress getAddressById(Long id) {
        TbAddress tbAddress = tbAddressMapper.selectByPrimaryKey(id);
        String province = tbProvincesMapper.selectProvinceByProvinceId(tbAddress.getProvinceId());
        String city = tbCitiesMapper.selectCityByCityId(tbAddress.getCityId());
        String area = tbAreasMapper.selectAreaByTownId(tbAddress.getTownId());
        tbAddress.setProvince(province);
        tbAddress.setCity(city);
        tbAddress.setTown(area);
        return tbAddress;
    }

    @Override
    public int updateAddress(TbAddress tbAddress) {
        return tbAddressMapper.updateByPrimaryKeySelective(tbAddress);
    }

    @Override
    public int deleteAddress(Long id) {
        return tbAddressMapper.deleteByPrimaryKey(id);
    }
}
